﻿using BootyBay.PrivateStoreApp.AgentServicesRef;
using BootyBay.PrivateStoreApp.Common;
using BootyBay.PrivateStoreApp.DataModel;
using BootyBay.PrivateStoreApp.StoreServicesRef;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;

namespace BootyBay.PrivateStoreApp.ViewModels
{
    public class AppCategoriesPageVM : BindableBase
    {
        #region Instance
        private static AppCategoriesPageVM instance = null;
        public static AppCategoriesPageVM Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new AppCategoriesPageVM();
                }
                return instance;
            }
        }

        #endregion

        #region 对象声明

        StoreServicesClient svcClient;
        AgentSvcClient agentClient;
        private ObservableCollection<Category> appCategories = new ObservableCollection<Category>();

        public ObservableCollection<Category> AppCategories
        {
            get { return appCategories; }
        }
        #endregion

        public AppCategoriesPageVM()
        {
            svcClient = new StoreServicesClient();
            agentClient = new AgentSvcClient();
        }

        #region 从服务器获取所有类别信息
        public async void LoadAllCategories()
        {
            appCategories.Clear();
            var allAppCategories = new ObservableCollection<Category>();
            string str = "";
            try
            {
                allAppCategories = await svcClient.GetAllCategoriesAsync();
            }
            catch (Exception ex)
            {
                str = ex.ToString();
            }
            if (str != "")
            {
                MessageDialog msg = new MessageDialog(str);
                await msg.ShowAsync();
            }
            if (allAppCategories != null)
            {
                foreach (Category item in allAppCategories)
                {
                    appCategories.Add(item);
                }
            }

        }
        #endregion

        #region 根据类别查询应用
        public async Task<ObservableCollection<UserAppDataModel>> GetAppsByCategories(int categories)
        {
            ObservableCollection<UserAppDataModel> apps = new ObservableCollection<UserAppDataModel>();
            //从服务器获取全部数据
            //var allAppData_list = await svcClient.GetAllAppInfoDataAsync();
            foreach (var item in AppListPageVM.Instance.AppData)
            {
                //UserAppDataModel usermodel = new UserAppDataModel(item);
                if (item.AppView.Category == categories)
                {
                    apps.Add(item);
                }
            }
            return apps;
        }
        #endregion

    }
}
